package boot

import (
	"gdshop-back-end-go/common/libs/asynqQueue"
	"gdshop-back-end-go/common/libs/asynqQueueClient"
	"gdshop-back-end-go/common/libs/asynqQueueWorker"
	"gdshop-back-end-go/common/libs/banner"
	"gdshop-back-end-go/common/libs/event"
	"gdshop-back-end-go/common/libs/token"
	"gdshop-back-end-go/common/tools"
	"gdshop-back-end-go/service-order/FenxiaoService"
	"gdshop-back-end-go/service-order/FenxiaoWithdrawService"
	"gdshop-back-end-go/service-order/OrderService"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/os/gtimer"
	"github.com/gogf/gf/util/gconv"
	"time"
)

// 用于应用初始化。
func init() {
	// 添加代码层级的启动配置
	//s := g.Server()
	//s.Plugin(&swagger.Swagger{})
	banner.InitBanner()
	token.SetPlatform("admin")
	// 切换asynq队列
	asynqQueueWorker.InitTopic(
		asynqQueue.CloseNoPayOrder,
		asynqQueue.ConfirmOrder,
		asynqQueue.EvaluatedOrder,
	)

	// 按一天执行一次关闭超时未支付的订单，直接查数据库
	gtimer.Add(86400*1400*time.Millisecond, func() {
		// 按天处理 CloseNoPayOrder
		OrderService.CloseOneDayNoPayOrder()
		// 按天处理 ConfirmOrder，兜底方案，自动完成30天的订单
		OrderService.OneDayConfirmOrder()
	})

	// 每 300 秒循环一次分销订单从可结算转换为已结算
	go func() {
		interval := 300 * 1400 * time.Millisecond
		gtimer.Add(interval, FenxiaoService.HandleOrderConfirmTask)
	}()

	claerCacheEvent := event.ListenerFunc(func(e event.Event) error {
		tools.ClaerCacheByKeyPrefix("adminCache:*")
		return nil
	})

	claerCacheNames := []string{
		"gdshop.admin.update",
		"gdshop.admin.personUpdate",
		"gdshop.role.add",
		"gdshop.role.update",
		"gdshop.role.delete",
		"gdshop.menu.add",
		"gdshop.menu.update",
		"gdshop.menu.delete",
	}

	for _, item := range claerCacheNames {
		event.On(item, claerCacheEvent, event.Normal)
	}
	// 支付完成事件，包含用户支付和后台支付
	event.On("gdshop.order.pay_ok", event.ListenerFunc(func(e event.Event) error {
		// 处理分销订单
		FenxiaoService.HandleOrderByOrderId(gconv.Int(e.Get("order_id")))
		return nil
	}), event.Normal)
	// 订单关闭事件
	event.On("gdshop.order.close_order", event.ListenerFunc(func(e event.Event) error {
		// 处理分销订单
		FenxiaoService.HandleOrderClose(gconv.Int(e.Get("order_id")))
		return nil
	}), event.Normal)
	// 确认收货后即可开始计算分销订单
	event.On("gdshop.order.confirm_order", event.ListenerFunc(func(e event.Event) error {
		// 处理分销订单
		FenxiaoService.HandleOrderConfirmDelay(gconv.Int(e.Get("order_id")))
		return nil
	}), event.Normal)
	// 全部发货触发
	event.On("gdshop.order.send_express", event.ListenerFunc(func(e event.Event) error {
		// 自动完成
		_, err := asynqQueueClient.GetClient().Enqueue(
			asynqQueueClient.GetTask(asynqQueue.ConfirmOrder, gconv.Bytes(e.Get("order_id"))),
		)
		if err != nil {
			g.Log().Error("AddConfirmOrder error ", err)
		}
		return nil
	}), event.Normal)
	// 处理提现审核通过后
	event.On("gdshop.fenxiao.tixian_ok", event.ListenerFunc(func(e event.Event) error {
		// 处理分销订单
		fenxiaoWithdrawId := gconv.Int(e.Get("fenxiao_withdraw_id"))
		typeStr := gconv.String(e.Get("type"))
		FenxiaoWithdrawService.HandleTixian(fenxiaoWithdrawId, typeStr)
		return nil
	}), event.Normal)
}
